package com.ktgj.flight.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.flight.domain.vo.TicketRemindVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.flight.domain.TicketRemind;
import com.ktgj.flight.service.ITicketRemindService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 提醒信息Controller
 * 
 * @author ruoyi
 * @date 2021-08-30
 */
@ControllerAdvice
@RestController
@RequestMapping("/remind")
public class TicketRemindController extends BaseController
{
    @Autowired
    private ITicketRemindService ticketRemindService;

    /**
     * 查询提醒信息列表
     */
    @PreAuthorize(hasPermi = "flight:remind:list")
    @GetMapping("/list")
    public TableDataInfo list(TicketRemind ticketRemind)
    {
        startPage();
        List<TicketRemind> list = ticketRemindService.selectTicketRemindList(ticketRemind);
        return getDataTable(list);
    }

    /**
     * 查询儿童婴儿购票说明
     * @return
     */
    @PostMapping("/queryTicketRemind")
    public AjaxResult queryTicketRemind() {
        return AjaxResult.success(ticketRemindService.queryTicketRemind());
    }

    /**
     * 导出提醒信息列表
     */
    @PreAuthorize(hasPermi = "flight:remind:export")
    @Log(title = "提醒信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TicketRemind ticketRemind) throws IOException
    {
        List<TicketRemind> list = ticketRemindService.selectTicketRemindList(ticketRemind);
        ExcelUtil<TicketRemind> util = new ExcelUtil<TicketRemind>(TicketRemind.class);
        util.exportExcel(response, list, "提醒信息数据");
    }

    /**
     * 获取提醒信息详细信息
     */
    @PreAuthorize(hasPermi = "flight:remind:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ticketRemindService.selectTicketRemindById(id));
    }

    /**
     * 新增提醒信息
     */
    @PreAuthorize(hasPermi = "flight:remind:add")
    @Log(title = "提醒信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TicketRemind ticketRemind)
    {
        return toAjax(ticketRemindService.insertTicketRemind(ticketRemind));
    }

    /**
     * 修改提醒信息
     */
//    @PreAuthorize(hasPermi = "flight:remind:edit")
//    @Log(title = "提醒信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody TicketRemind ticketRemind) {
        int i = ticketRemindService.updateTicketRemind(ticketRemind);
        if (i > 0){
            return AjaxResult.success("修改成功");
        }
        else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除提醒信息
     */
    @PreAuthorize(hasPermi = "flight:remind:remove")
    @Log(title = "提醒信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ticketRemindService.deleteTicketRemindByIds(ids));
    }

    /**
     * 删除提醒信息
     */
    @PostMapping("/delRemove")
    public AjaxResult delRemove(@RequestBody Map<String, Object> map) {
        Long id = Long.valueOf(map.get("id").toString());
        int i = ticketRemindService.deleteTicketRemindById(id);
        if (i > 0){
            return AjaxResult.success("删除成功");
        }
        else {
            return AjaxResult.error("删除失败");
        }
    }

    /**
     * 机票提醒信息列表
     * */
    @PostMapping("/selectTicketRemindList")
    public TableDataInfo selectTicketRemindList(@RequestBody Map<String, Object> map) {
        startPage();
        String state = null;
        String remindType = null;
        String remindName = null;
        String time = null;
        String timeBack = null;
        if (map.get("state") != null && !map.get("state").equals("")){
            state = map.get("state").toString();
        }
        if (map.get("remindType") != null && !map.get("remindType").toString().equals("")){
            remindType = map.get("remindType").toString();
        }
        if (map.get("name") != null && !map.get("name").equals("")){
            remindName = map.get("name").toString();
        }
        if (map.get("time") != null && !map.get("time").equals("")){
            time = map.get("time").toString();
        }
        if (map.get("timeBack") != null && !map.get("timeBack").equals("")){
            timeBack = map.get("timeBack").toString();
        }
        List<TicketRemind> ticketReminds = ticketRemindService.selectTicketRemindList(state, remindType, remindName, time, timeBack);
        return getDataTable(ticketReminds);
    }

    /**
     * 创建机票提醒消息信息
     * */
    @PostMapping("/addTicketRemind")
    public AjaxResult addTicketRemind(HttpServletRequest request, @RequestBody TicketRemindVo ticketRemindVo){
        int i;
        try {
            i = ticketRemindService.addTicketRemind(request, ticketRemindVo);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("创建成功");
        }
        else {
            return AjaxResult.error("创建失败");
        }
    }
}
